package daobeauty.interceptor.invoke.parser.operator;

import java.util.List;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

import daobeauty.util.Stack;

public class GtOperator implements Operator {
	private String propertyName;
	private Class argumentType;
	
	public GtOperator(String propertyName, Class argumentType) {
		this.propertyName = propertyName;
		this.argumentType = argumentType;
	}

	public Criterion createCriterion(Stack argumentStack) {
		final Object argument = argumentStack.pop();
		if (argument==null){
			return Restrictions.isNull(propertyName);
		}
		return Restrictions.gt(propertyName, argument);
	}
	
	@SuppressWarnings("unchecked")
	public String toHqlQuery(Stack argumentStack, List usedArguments){
		final Object argument = argumentStack.pop();
		if (argument==null){
			return "d."+propertyName+" IS NULL";
		}
		usedArguments.add(argument);
		return "d."+propertyName+" > ?";
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((argumentType == null) ? 0 : argumentType.hashCode());
		result = prime * result
				+ ((propertyName == null) ? 0 : propertyName.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final GtOperator other = (GtOperator) obj;
		if (argumentType == null) {
			if (other.argumentType != null)
				return false;
		} else if (!argumentType.equals(other.argumentType))
			return false;
		if (propertyName == null) {
			if (other.propertyName != null)
				return false;
		} else if (!propertyName.equals(other.propertyName))
			return false;
		return true;
	}

	public int getAmountArguments() {
		return 1;
	}

	public int getTotalAmountArguments() {
		return 1;
	}

	public String toTotalString() {
		return propertyName+" > "+argumentType.getName();
	}
	
	public String toString(){
		return toTotalString();
	}
}
